/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.greenbridge.binder.excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Cell;
import jxl.Sheet;

/**
 *
 * This class should really be brought into the Greenbridge core.
 * @author u368337
 */
public class NamedColumnToMapGenerator {

    protected Sheet sheet;
    private Map<String,Integer> nameToColumn = new HashMap<String,Integer>();

    public NamedColumnToMapGenerator(Sheet sheet) {
        this.sheet = sheet;
        readNames(sheet);
    }


    public Map generateNameToCellMap(Cell[] row) {
        Map<String,String> result = new HashMap<String, String>();
        for (String name : nameToColumn.keySet()) {
            Integer col = nameToColumn.get(name);
            result.put(name, row[col].getContents());
        }

        return result;
    }


    protected void readNames(Sheet sheet) {
        Cell[] cols = sheet.getRow(0);
        for (int i = 0; i < cols.length; i++) {
            Cell cell = cols[i];
            if (cell.getContents().startsWith("_")) {
                String name = cell.getContents().substring(1);
                nameToColumn.put(name, i);
            }
        }
    }



}
